package com.example.golfwithfriends;

import java.util.ArrayList;
import java.util.List;

import com.golfwithfriends.controller.ParcourController;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.View;

public class NouveauJeu extends Activity implements View.OnClickListener {
	
	private int nbJoueurAdded = 0;
	private static final int MAX_PLAYER = 5;
	
	private Button addPlayer = null;
	private Button startGame = null;
	private List<TextView> playerList = null;
	private EditText playerName = null;
	private List<String> players = null;
	private Spinner nbTrouSpinner = null;
	private ListView playerListView = null;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nouveau_jeu);
        
        addPlayer = (Button) findViewById(R.id.AddPlayer);
        startGame = (Button) findViewById(R.id.StartGame);
        playerName = (EditText) findViewById(R.id.PlayerName);
        nbTrouSpinner = (Spinner) findViewById(R.id.NbTrous);
        players = new ArrayList<String>();
        
        playerList = new ArrayList<TextView>();
        playerList.add((TextView) findViewById(R.id.joueur1));
        playerList.add((TextView) findViewById(R.id.Joueur2));
        playerList.add((TextView) findViewById(R.id.Joueur3));
        playerList.add((TextView) findViewById(R.id.Joueur4));
        playerList.add((TextView) findViewById(R.id.Joueur5)); 
        
        addPlayer.setOnClickListener(this);
        startGame.setOnClickListener(this);
        nbTrouSpinner.setSelection(8);
        startGame.setEnabled(false);
        
        playerListView = (ListView) findViewById(R.id.Main_PlayerListView);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nouveau_jeu, menu);
        return true;
    }


	@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		case R.id.AddPlayer:
			if (nbJoueurAdded < MAX_PLAYER)
			{
				String pn = playerName.getText().toString();
				if (pn != "")
				{
					playerList.get(nbJoueurAdded).setText(pn);
					nbJoueurAdded++;
					playerName.setText("");
					players.add(pn);
					TextView tvp = new TextView(getApplicationContext());
					tvp.setText(pn);
					playerListView.addView(tvp);
				}
				if (nbJoueurAdded == 5)
				{
					addPlayer.setEnabled(false);
				}
			}
			if (nbJoueurAdded > 0)
				startGame.setEnabled(true);
			break;
		case R.id.StartGame:
			if (nbJoueurAdded > 0)
			{
				TextView selectedHole = (TextView) nbTrouSpinner.getSelectedView();
				String result[] = ((String) selectedHole.getText()).split(" ");
				int nbHoles = Integer.valueOf(result[0]);
				ParcourController.getInstance().initParcour(nbHoles, players);
				
				Intent intent = new Intent(this, HoleActivity.class);
				startActivity(intent);
			}
			break;
		}
	}
    
}
